package com.hudson.core.base;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseComponent {

    // 使用ThreadLocal确保线程安全
    protected static final ThreadLocal<StopWatch> stopWatch = new ThreadLocal<StopWatch>() {
        @Override
        protected StopWatch initialValue() {
            return new StopWatch();
        }
    };

    protected Logger log = LoggerFactory.getLogger(getClass());

    public static void startStopWatch() {
        stopWatch.get().start();
    }

    public static long getTimeByMs() {
        return stopWatch.get().stop();
    }

    public static double getTimeByS() {
        return stopWatch.get().stop() / 1000.0;
    }

    // 如果需要在实例方法中使用，也可以提供实例方法
    public void startStopWatchInstance() {
        startStopWatch();
    }

    public long getTimeByMsInstance() {
        return getTimeByMs();
    }

    public double getTimeBySInstance() {
        return getTimeByS();
    }
}
